Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix email notification reply to #603

Merged
merged 1 commit into from
Oct 28, 2024
Merged

Conversation

chiragchhatrala
Copy link
Collaborator

@chiragchhatrala chiragchhatrala commented Oct 25, 2024

Summary by CodeRabbit

  • New Features

    • Improved email notification handling with conditional link creation.
    • Emails now use the creator's email as the reply-to address when applicable.
  • Bug Fixes

    • Enhanced reliability of email notifications regarding reply addresses.
  • Tests

    • Added new test cases to verify email functionality with mention and empty reply-to fields.
    • Updated existing tests to ensure correct handling of reply addresses in email notifications.

Copy link
Contributor

coderabbitai bot commented Oct 25, 2024

Walkthrough

The changes in this pull request involve significant modifications to the FormEmailNotification class and its associated test suite. The formattedData property has been removed, with its functionality replaced by a revised formatSubmissionData() method that includes a new boolean parameter for conditional link creation. The email handling logic has been updated to utilize the creator's email from the event context. Additionally, the test suite has been enhanced with new test cases and updates to existing ones to improve coverage regarding email notifications and reply address handling.

Changes

File Change Summary
api/app/Notifications/Forms/FormEmailNotification.php - Removed formattedData property.
- Updated formatSubmissionData() to accept a new boolean parameter $createLinks.
- Modified toMail() to use event creator's email.
- Simplified getReplyToEmail() method.
- Updated getMailData() and getEmailContent() to use formatSubmissionData().
- Removed getRespondentEmail() method.
api/tests/Feature/Forms/EmailNotificationTest.php - Added new test case: send email with mention as reply to.
- Added new test case: send email with empty reply to.
- Updated existing test case: send email with the submitted data to assert replyTo field.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant NotificationService
    participant EmailService

    User->>NotificationService: Trigger email notification
    NotificationService->>EmailService: Prepare email using formatSubmissionData(createLinks=false)
    EmailService-->>NotificationService: Email prepared with creator's email
    NotificationService-->>User: Email sent confirmation
Loading

🐰 "In the garden of code, changes bloom bright,
With emails now clear, and links shining light.
From formattedData, we’ve taken a leap,
To handle replies, our logic runs deep.
So hop with delight, let notifications sing,
For every new feature, joy it will bring!" 🐇


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (4)
api/app/Notifications/Forms/FormEmailNotification.php (2)

64-72: Consider adding type declaration for $createLinks parameter

The implementation looks good and provides flexibility for link creation. Consider adding a type declaration for better code clarity.

-private function formatSubmissionData($createLinks = true): array
+private function formatSubmissionData(bool $createLinks = true): array

119-119: Document the createLinks parameter usage pattern

The usage of formatSubmissionData(false) for subjects and formatSubmissionData() for content is correct, but it would be helpful to document this pattern.

Add a comment to the formatSubmissionData method:

+    /**
+     * Format the submission data for display
+     * 
+     * @param bool $createLinks When true, URLs in the data will be converted to clickable links.
+     *                         Set to false when formatting data for subjects or reply-to addresses.
+     * @return array
+     */
     private function formatSubmissionData($createLinks = true): array

Also applies to: 151-151, 161-161

api/tests/Feature/Forms/EmailNotificationTest.php (2)

168-197: Consider adding more assertions for comprehensive testing.

While the test correctly verifies the reply-to address, consider adding assertions for:

  • The mention syntax parsing
  • Edge cases where the mentioned field is empty or invalid

Example additions:

// Add these assertions
expect($renderedMail->replyTo)->toHaveCount(1);
expect($formData[$emailProperty['id']])->toBe('[email protected]');

199-228: Improve test clarity and robustness.

A few suggestions to enhance this test:

  1. The form data includes an email that isn't used in the test, which might be confusing
  2. The creator's email could be verified more explicitly

Consider this refactor:

-    $formData = [
-        $emailProperty['id'] => '[email protected]',
-    ];
+    $formData = FormSubmissionDataFactory::generateSubmissionData($form);
+    
+    // Explicitly verify the creator's email before using it
+    expect($form->creator->email)->not->toBeNull();
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 6bdce23 and 72025c0.

📒 Files selected for processing (2)
  • api/app/Notifications/Forms/FormEmailNotification.php (5 hunks)
  • api/tests/Feature/Forms/EmailNotificationTest.php (2 hunks)
🔇 Additional comments (2)
api/app/Notifications/Forms/FormEmailNotification.php (1)

55-55: LGTM: Improved reply-to email handling

Using the form creator's email as the fallback ensures there's always a valid reply-to address, which is a robust improvement.

api/tests/Feature/Forms/EmailNotificationTest.php (1)

29-29: LGTM! Good test coverage addition.

The new assertion properly verifies that the reply-to address is correctly set in the email notification.

if ($replyTo) {
$parsedReplyTo = $this->parseReplyTo($replyTo);
if ($parsedReplyTo && $this->validateEmail($parsedReplyTo)) {
return $parsedReplyTo;
}
}

return $this->getRespondentEmail() ?? $default;
return $default;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider adding logging for invalid reply-to addresses

While the reply-to handling is improved, it would be helpful to log when an invalid reply-to address is provided to assist with debugging configuration issues.

 private function getReplyToEmail($default): string
 {
     $replyTo = $this->integrationData->reply_to ?? null;
     if ($replyTo) {
         $parsedReplyTo = $this->parseReplyTo($replyTo);
-        if ($parsedReplyTo && $this->validateEmail($parsedReplyTo)) {
+        if ($parsedReplyTo && $this->validateEmail($parsedReplyTo)) {
             return $parsedReplyTo;
+        } elseif ($parsedReplyTo) {
+            \Log::warning('Invalid reply-to email configured', [
+                'form_id' => $this->event->form->id,
+                'configured_reply_to' => $replyTo,
+                'parsed_reply_to' => $parsedReplyTo
+            ]);
         }
     }
     return $default;
 }

Also applies to: 112-113

@JhumanJ JhumanJ merged commit 08837e6 into main Oct 28, 2024
5 checks passed
@JhumanJ JhumanJ deleted the Fix-email-notification-reply-to branch October 28, 2024 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants